const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: ['../../img/no/banner.png', '../../img/no/banner.png'],
    products:[],
    pageSize: 10,
    pageNumber: 1
  },

  // 修复swiper高度不能自适应问题
  imageLoad: function (e) {
    var res = wx.getSystemInfoSync();
    var imgwidth = e.detail.width,
      imgheight = e.detail.height + 44 * res.windowWidth / 750,
      ratio = imgwidth / imgheight;
    this.setData({
      bannerHeight: res.windowWidth / ratio
    })
  },

  // 跳转到自驾
  toSelfDriv: function () {
    wx.navigateTo({
      url: '../self-driv/self-driv'
    })
  },

  // 跳转到静展
  toShow: function () {
    wx.navigateTo({
      url: '../show/show'
    })
  },

  // 跳转到婚庆
  toHunQ: function () {
    wx.navigateTo({
      url: '../hunqing/hunqing'
    })
  },

  // 跳转到详情
  toDetails: function (e) {
    var productId = e.currentTarget.dataset.value
    wx.navigateTo({
      url: '../details/details?productId='+productId
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadProduct()
    this.loadImages()
    if (app.globalData.userInfo && app.globalData.userInfo != '') {
      this.setData({
        albumDisabled: false,
        bindDisabled: true
      });
    } else {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = userInfo => {
        this.setData({
          userInfo: userInfo
        })

        if (userInfo.isLogin != 1) {
          wx.redirectTo({
            url: '../login/login'
          })
        }
        if (userInfo != '') {
          this.setData({
            albumDisabled: false,
            bindDisabled: true
          });
        }
      }

    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let pageNumber = this.data.pageNumber

    pageNumber = pageNumber + 1
    this.setData({
      pageNumber: pageNumber
    });
    let that = this
    //加载推荐商品
    wx.request({
      url: 'https://small.qiongyazuche.com/sunday/small/product/select',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        'isIndexRecommend': 1,
        'pageSize': that.data.pageSize,
        'pageNumber': that.data.pageNumber,
        'sort': 'sequence',
        'order': 'asc'

      },
      success: function (res) {

        let products = that.data.products.concat(res.data.result)

        that.setData({
          products: products
        })
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '琼崖租车',
      path: '/pages/index/index'
    }
  }, 
   loadProduct: function () {
    let that = this
    //加载推荐商品
    wx.request({
      url: 'https://small.qiongyazuche.com/sunday/small/product/select',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        'isIndexRecommend': 1,
        'pageSize': that.data.pageSize,
        'pageNumber': that.data.pageNumber,
        'sort':'sequence',
        'order':'asc'

      },
      success: function (res) {

        let products = that.data.products.concat(res.data.result)
        that.setData({
          products: products
        })
      }
    })
 
  }, loadImages: function () {
    let that = this
    //加载推荐商品
    wx.request({
      url: 'https://small.qiongyazuche.com/sunday/small/advertisement/getAdvertisementByZoneKey',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        'zoneKey': 'index_roll'

      },
      success: function (res) {
        that.setData({
          images: res.data.result
        })
      }
    })
  }
})